<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float: right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float: right; margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="输入搜索：">
            <el-input
              v-model="listQuery.orderNum"
              class="input-width"
              placeholder="订单编号"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户手机号：">
            <el-input
              v-model="listQuery.phone"
              class="input-width"
              placeholder="手机号"
            ></el-input>
          </el-form-item>
          <el-form-item label="订单状态：">
            <el-select
              v-model="listQuery.status"
              class="input-width"
              placeholder="全部"
              clearable
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>
    <div class="table-container">
      <el-table
        ref="orderTable"
        :data="list"
        style="width: 100%"
        v-loading="listLoading"
        border
      >
        <el-table-column label="订单编号" align="center">
          <template slot-scope="scope">{{ scope.row.orderNum }}</template>
        </el-table-column>
        <el-table-column label="产品名称" align="center">
          <template slot-scope="scope">{{ scope.row.productName }}</template>
        </el-table-column>
        <el-table-column label="用户手机号" align="center" prop="phone">
        </el-table-column>

        <el-table-column label="微信支付订单" align="center">
          <template slot-scope="scope">{{ scope.row.wxPayOrderNum }}</template>
        </el-table-column>
        <el-table-column label="总金额（元）" align="center">
          <template slot-scope="scope"
            >￥{{ scope.row.totalAmount | formatCentToYuan }}</template
          >
        </el-table-column>
        <el-table-column label="支付金额（元）" align="center">
          <template slot-scope="scope"
            >￥{{ scope.row.payAmount | formatCentToYuan }}</template
          >
        </el-table-column>
        <el-table-column label="支付方式" align="center">
          <template slot-scope="scope">{{
            scope.row.payType | formatPayType
          }}</template>
        </el-table-column>
        <el-table-column label="代理人组织" align="center">
          <template slot-scope="scope">{{
            scope.row.organizationName
          }}</template>
        </el-table-column>
        <el-table-column label="代理人手机号" align="center">
          <template slot-scope="scope">{{ scope.row.promoterPhone }}</template>
        </el-table-column>
        <el-table-column label="订单状态" align="center">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.status === 0">初始化</el-tag>
            <el-tag v-else-if="scope.row.status === 1">待付款</el-tag>
            <el-tag type="success" v-else-if="scope.row.status === 2"
              >已完成</el-tag
            >
            <el-tag type="danger" v-else>无效订单</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="支付时间" align="center">
          <template slot-scope="scope">{{
            scope.row.paymentTime | formatCreateTime
          }}</template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleViewOrder(scope.$index, scope.row)"
              >查看订单</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="PAGE_SIZES"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { fetchList } from "@/api/order";
import { formatDate } from "@/utils/date";
import { formatCentToYuan } from "@/utils/format";
import { PAGE_SIZES, DEFAULT_PAGE_SIZE } from "@/utils/constant/system";

const defaultListQuery = {
  pageNum: 1,
  pageSize: DEFAULT_PAGE_SIZE,
  orderNum: null,
  status: null,
  phone: ""
};
export default {
  name: "orderList",
  data() {
    return {
      PAGE_SIZES,
      listQuery: Object.assign({}, defaultListQuery),
      listLoading: true,
      list: null,
      total: null,
      operateType: null,
      multipleSelection: [],
      closeOrder: {
        dialogVisible: false,
        content: null,
        orderIds: [],
      },
      statusOptions: [
        {
          label: "待付款",
          value: 0,
        },
        {
          label: "待发货",
          value: 1,
        },
        {
          label: "已发货",
          value: 2,
        },
        {
          label: "已完成",
          value: 3,
        },
        {
          label: "已关闭",
          value: 4,
        },
        {
          label: "无效订单",
          value: 5,
        },
      ],
      logisticsDialogVisible: false,
    };
  },
  created() {
    this.getList();
  },
  filters: {
    formatCentToYuan,
    formatCreateTime(time) {
      if (!time) return "";
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
    },
    formatPayType(value) {
      if (value === 0) {
        return "未支付";
      } else if (value === 1) {
        return "微信支付";
      } else {
        return "";
      }
    },
  },
  methods: {
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleViewOrder(index, row) {
      this.$router.push({
        path: "/oms/orderDetail",
        query: { id: row.orderNum },
      });
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
  },
};
</script>
<style scoped lang="scss">
.input-width {
  width: 203px;
}
</style>
